package com.ruoyi.web.controller.comlbs;


import com.alibaba.fastjson2.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.BigDataConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.Page;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BigDataException;
import com.ruoyi.common.utils.http.CommonResponse;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.system.domain.OrderSendRecord;
import com.ruoyi.system.domain.request.driver.DriverFaceSendParam;
import com.ruoyi.system.domain.vo.order.OrderSendGroupRecord;
import com.ruoyi.system.service.IOrderSendRecordService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 指令发送记录Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@Slf4j
@RestController
@RequestMapping("/comlbs/orderSendRecord")
public class OrderSendRecordController extends BaseController {
    @Autowired
    private IOrderSendRecordService orderSendRecordService;
    @Autowired
    private BigDataConfig bigDataConfig;

    @ApiOperation("指令发送组列表")
    @ApiOperationSupport(includeParameters = {"startTime","endTime","orderId"})
    @GetMapping("/group/list")
    public TableDataInfo groupList(OrderSendRecord orderSendRecord) {
        startPage();
        List<OrderSendGroupRecord> list = orderSendRecordService.selectGroupList(orderSendRecord);
        return getDataTable(list);
    }

    @ApiOperation("指令发送详情列表")
    @ApiOperationSupport(includeParameters = {"groupId","orderId","vehicleNum","status","orderType"})
    @GetMapping("/list")
    public AjaxResult detailList(OrderSendRecord orderSendRecord) {
        return success(orderSendRecordService.selectList(orderSendRecord));
    }

    @ApiOperation("设备指令下发列表")
    @ApiOperationSupport(includeParameters = {"deviceNo","startTime","endTime","orderName"})
    @GetMapping("/device/list")
    public TableDataInfo deviceList(OrderSendRecord orderSendRecord) {
        startPage();
        Page<JSONObject> page = orderSendRecordService.deviceList(orderSendRecord);
        return getDataTable(page.getList(), page.getTotal());
    }

    /**
     * 获取指令发送记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderSendRecordService.getById(id));
    }

    @ApiOperation("新增指令发送记录")
    @ApiOperationSupport(includeParameters = {"orderId","vehicleId","vehicleNum","deviceNo","content","confValue"})
    @Log(title = "指令发送记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<OrderSendRecord> orderSendRecordList) {
        return success(orderSendRecordService.add(orderSendRecordList));
    }

    @ApiOperation("发送指令")
    @Log(title = "指令发送", businessType = BusinessType.INSERT)
    @PostMapping("/sendOrder")
    public AjaxResult sendOrder(@RequestBody List<JSONObject> orderList) {
        for (int i = 0; i < orderList.size(); i++) {
            CommonResponse<Object> response = HttpRequest.postJson(bigDataConfig.getGatewayApiUrl() + "/KafkaApiController/distributeAll", orderList.get(i), Object.class);
            if (!response.isSuccess()) throw new BigDataException(String.format("共%d条指令，第%d条指令发送失败", orderList.size(), i + 1));
        }
        return success();
    }

    @ApiOperation("重新发送指令")
    @Log(title = "指令发送", businessType = BusinessType.INSERT)
    @PostMapping("/againSendOrder")
    public AjaxResult againSendOrder(Long[] ids) {
        return success(orderSendRecordService.againSendOrder(ids));
    }

    @Anonymous
    @ApiOperation("更新指令发送结果")
    @Log(title = "更新指令发送结果", businessType = BusinessType.UPDATE, isAnonymous = true)
    @PostMapping("/updateOrderResult")
    public AjaxResult updateOrderResult(@RequestBody OrderSendRecord param) {
        param.setUpdateBy(UserConstants.ADMIN_ID);
        log.info("更新指令发送结果：" + param.toJsonObject());
        return toAjax(orderSendRecordService.updateById(param));
    }

}
